<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>Nginx 配置 Combo 合并 HTTP 请求 | 前端性能优化</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    
    <link rel="prev" href="./good-practices-for-the-different-network-types.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="gitbook/style.css">


        
    <div class="book"  data-level="7" data-basepath="." data-revision="1510986466881">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                         全栈视野的网页优化
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="common-optimization-indicator.html">
            
                
                    <a href="./common-optimization-indicator.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         常用的性能优化指标
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="yahooperformancerules.html">
            
                
                    <a href="./yahooperformancerules.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         依旧有效的雅虎优化法则
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="performance-optimization-tool-using-actual-combat.html">
            
                
                    <a href="./performance-optimization-tool-using-actual-combat.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         性能优化工具实战
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="cache-http-protocol-header-field.html">
            
                
                    <a href="./cache-http-protocol-header-field.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         HTTP缓存
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="ondemand-resource-loading.html">
            
                
                    <a href="./ondemand-resource-loading.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         按需加载资源
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="good-practices-for-the-different-network-types.html">
            
                
                    <a href="./good-practices-for-the-different-network-types.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         不同网络类型的最佳实践
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="7" data-path="configure-nginx-combo-with-http-request.html">
            
                
                    <a href="./configure-nginx-combo-with-http-request.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         Nginx 配置 Combo 合并 HTTP 请求
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >前端性能优化</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_159">
                    
                        <h2 id="nginx--combo--http-">优化案例：　Nginx 配置 Combo 合并 HTTP 请求</h2>
<p>　　Nginx 是一个开源高效的 HTTP 服务器，通过简单的配置能够提供丰富的功，最重要的是，他对服务器的配置要求极低。Nginx 选用了有事驱动的异步非阻塞模型，而不是传统的依赖多线程来响应，因此，在同等资源的情况下能够提供极大的并发请求。对于前端开发者来说，选用 Nginx 是一个主流并且高性价比的选择。</p>
<h3 id="71--nginx-">7.1 安装 Nginx 和文件合并模块</h3>
<p>　　要玩转 Nginx，首先推荐使用 Linux 操作系统的服务器来搭建服务，一台普通的云服务器即可。文件合并模快选择开源插件 nginx-http-concat, 操作系统为 CentOS 7。一般情况下安装 Nginx 推荐添加 EPEL (全称 Extra Package for Enterprise Linux, 即企业级 Linux 附加包)安装源，然后通过 CentOS 包管理器 yum 安装。这种方式的安装简单且方便升级。因为安装模块需要重行编译 Nginx 安装包，所以这里介绍通过二进制包来安装，步骤如下。</p>
<ol>
<li><p>下最新的 Nginx 稳定版本二进制包，以版本 1.10.3　为例, 命令如下：</p>
<pre><code>wget http://nginx.org/download/nginx-1.10.3.tar.g
</code></pre></li>
<li><p>解压缩安装包，命令如下：</p>
<pre><code>tar zxvf nginx-1.10.3.tar.gz
</code></pre></li>
<li><p>克隆创酷镜像，命令如下：</p>
<pre><code>git clone git@github.com:alibaba/nginx-http-concat.git
</code></pre></li>
<li><p>切换到 nginx 目录，命令如下：</p>
<pre><code>cd nginx-1.10.3
</code></pre></li>
<li><p>在构建参数中添加模块并构建，代码如下：</p>
<pre><code>./configure --add-module=/path/to/nginx-http-concat
make &amp;&amp; make install
</code></pre></li>
<li><p>查找 Nginx 地址，命令如下：</p>
<pre><code>// 找到 Nginx 执行文件的地址，可以配置命令行环境，或者通过路径调用
whereis nginx
// 查找到 Nginx 的默认配置文件地址，如果出现异常，默认在/etc/nginx/conf.d/default.conf
nginx -t
</code></pre></li>
</ol>
<p>通过 cat 命令读取配置文件，可以看到 root 地址，对应网站根目录。</p>
<ol>
<li>启动 Nginx, 命令如下：<pre><code>// 推荐使用systemctl来启动，也可以通过 nginx 命令来启动
sudo systemctl start nginx
//　开机自启动，可选
sudo systemctl enable nginx
</code></pre></li>
</ol>
<h2 id="72--nginx--combo">7.2 配置 Nginx 和 Combo</h2>
<p>　　nginx-http-concat Nginx 适用于 Nginx 的文件合并模块，可以将多个对静态资源的 HTTP 请求合并成为一个，进而减少 HTTP 请求数。假设原来需要 a.js, b.js, c.js 三个文件，产生三次请求，通过该模块可以在一个请求中完成，实例地址如下:</p>
<pre><code>//使用??作标识表示合并文件
http://example.com/static/??a.js,b.js,c.js
</code></pre><p>需要修改 Nginx 的配置，文件配置如下：</p>
<pre><code>location /static/ {
    # nginx-http-concat 主开关
    concat on
    # 最大合并文件数
    # concat_max_files 10；
    # 只允许通内省文件合并
    # concat_unique on
    # 允许合并的文件类型，多个以逗号分隔。如: application/x-javascript, text/css
    # concat_types text/html
}
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./good-practices-for-the-different-network-types.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 不同网络类型的最佳实践"><i class="fa fa-angle-left"></i></a>
        
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
